// pages/songDetail/songDetail.js
import {
  songDetail,
  getSongUrl
} from '../../api/songDetail/index';

import moment from 'moment';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay: false,
    recommendsongList: [], // 当前正在播放的列表
    songInfo: {},
    songUrl: '', // 歌曲播放地址
    currentTime: "00:00", // 当前播放时间
    durationTime: "00:00", // 总时间
    progressWidth: 0, // 进度条宽度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("页面显示...onLoad");
    this.eventChannel = this.getOpenerEventChannel();
    this.eventChannel.on('startPlay', (list, current) => {
      console.log("startPlay", list);
      console.log("startPlay", current);

      this.setData({
        recommendsongList: list,
        songInfo: current,
      });
    });
    // 创建控制音乐播放的实例
    this.backgroundAudioManager = wx.getBackgroundAudioManager();

    console.log("this.backgroundAudioManager:0", this.backgroundAudioManager);
    this.backgroundAudioManager.onEnded(() => {
      console.log("播放结束");
      this.setData({
        isPlay: false,
      })
    });
    this.backgroundAudioManager.onPause(() => {
      console.log("播放暂停");
      this.setData({
        isPlay: false,
      })
    });
    this.backgroundAudioManager.onPlay(() => {
      console.log("播放开始");
      this.setData({
        isPlay: true,
      })
    });
    this.backgroundAudioManager.onTimeUpdate(() => {
      let progressWidth = this.backgroundAudioManager.currentTime / this.backgroundAudioManager.duration * 450;
      this.setData({
        currentTime: moment(this.backgroundAudioManager.currentTime * 1000).format('mm : ss'),
        progressWidth
      });
    });
    this.getSongPlayUrl(options);

  },

  getSongPlayUrl: async function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    // 设置顶部显示
    wx.setNavigationBarTitle({
      title: this.data.songInfo.name || "音乐播放",
    })
    // 获取歌曲播放路径
    let songUrlResp = await getSongUrl({
      id: this.data.songInfo.id
    });
    wx.hideLoading();
    this.setData({
      songUrl: songUrlResp.data[0].url,
      durationTime: moment(this.data.songInfo.dt).format("mm : ss"),
    });
    // 如果当前播放的歌曲Id和之前一致, 不需要重新播放
    if (this.backgroundAudioManager.songId == this.data.songInfo.id) {
      return;
    }
    this.backgroundAudioManager.src = songUrlResp.data[0].url;
    this.backgroundAudioManager.title = this.data.songInfo.name;
    this.backgroundAudioManager.play();
    this.backgroundAudioManager.songId = this.data.songInfo.id;
  },

  handleMusicPlay: function () {
    if (this.data.isPlay) {
      this.backgroundAudioManager.pause();
    } else {
      console.log();
      this.backgroundAudioManager.play();
    }
  },

  handleSwitch: function (event) {
    let type = event.currentTarget.id;

    let index = this.data.recommendsongList.findIndex((item) => item.id == this.data.songInfo.id);
    console.log(index);
    if (type == 'prev') {
      if (index == 0) {
        index = this.data.recommendsongList.length;
      }
      index--;
    } else {
      if (index == this.data.recommendsongList.length - 1) {
        index = -1;
      }
      index++;
    }
    console.log("index:", index);
    this.setData({
      songInfo: this.data.recommendsongList[index]
    });
    this.getSongPlayUrl();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("页面显示onShow");
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("页面隐藏");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("页面卸载");
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})